package com.liyunc.demo.resources;
/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.liyunc.demo.model.dto.GenericDto;
import com.liyunc.demo.model.dto.UserInfoDto;
import com.liyunc.demo.model.entity.UserInfo;
import com.liyunc.demo.service.PermissionService;
import com.liyunc.demo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-02-16
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-02-16               liyuncong        Created
 */

@RestController
@RequestMapping(value = "/api/v1/user", produces = {MediaType.APPLICATION_JSON_VALUE})
@Validated
@Schema(description = "用户数据")
public class UserResource {

    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;

    @GetMapping(value = "/{userId}")
    @Operation(description = "获取单个用户")
    public GenericDto<UserInfo> getSingleUserByUserId(
        @Min(value = 1) @PathVariable("userId") long userId) {
        return GenericDto.of(userService.getSingleUser(userId).orElse(null));
    }

    @GetMapping(value = "")
    @Operation(description = "获取用户列表")
    public GenericDto<List<UserInfoDto>> userList(
        @Valid @Min(0) @RequestParam(value = "startIndex", defaultValue = "0") int startIndex,
        @Valid @Min(1) @RequestParam(value = "pageSize", defaultValue = "10") int pageSize
    ) {
        return GenericDto.of(userService.getUserList(startIndex, pageSize));
    }

    @PostMapping(value = "", consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Operation(description = "创建用户")
    public GenericDto<UserInfoDto> createUser(@RequestBody UserInfoDto userDto) {
        return GenericDto.of(userService.createUser(userDto).orElse(null));
    }

    @GetMapping(value = "/{userId}", params = {"action=delete"})
    @Operation(description = "删除用户")
    public GenericDto<Boolean> deleteSingleUser(
        @Min(value = 1) @PathVariable("userId") long userId) {
        userService.deleteUser(userId);
        return GenericDto.of(Boolean.TRUE);
    }
}
